/************************************************************
 * Project Name		[Thinking_In_Java]
 * File Name     	[pkg_api_testing.Java_Util_Scanner.java]
 * Creation Date 	[13-Jun-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_api_testing;

import java.util.Scanner;

/**
 * java.util.Scanner
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class Java_Util_Scanner {

	/**
	 * 从键盘上输入一个数字, 如果是素数则继续输入, 如果不是则退出程序
	 */
	public static void getPrimes() {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入要判断的数字");

		int i = 0;
		outer: while (true) {
			try {
				i = scanner.nextInt();
			} catch (Exception e) {
				System.out.println("输入内容无效, 请重新输入");
				break outer;
			}
			if (i == 0 || i == 1) {
				System.out.println("输入数字非法");
			} else {
				for (int j = 2; j < i; j++) {
					if (i % j == 0) {
						System.out.println(i + "不是素数, 程序退出");
						break outer;
					}
				}
				System.out.println(i + "是素数, 请继续");
			}
		}

		scanner.close();
	}

	/**
	 * 测试方法
	 * 
	 * @param args
	 *            命令行参数
	 */
	public static void main(String[] args) {
		Java_Util_Scanner.getPrimes();
	}
}
